/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CadMedico.java
 *
 * Created on 04/05/2012, 19:31:16
 */
package View;

import Dao.SistemaDAO;
import Modelo.Especialidade;
import Modelo.Login;
import Modelo.Medico;
import Modelo.Sala;
import Util.PAPUtil;
import java.util.ArrayList;
import javax.swing.ComboBoxModel;
import javax.swing.JOptionPane;

/**
 *
 * @author Marcelo Zanotelli
 */
public class CadMedico extends javax.swing.JFrame {

    LocMedico tp;
    Medico medico = new Medico();
    SistemaDAO sisDao = new SistemaDAO();
    PAPUtil papUtil = new PAPUtil();
    ArrayList<Especialidade> arrayEspecialidade = new ArrayList<Especialidade>();
    Login l;

    /**
     * Creates new form CadMedico
     */
    public CadMedico(LocMedico tp) {
        this.tp = tp;
        initComponents();
        this.setExtendedState(javax.swing.JFrame.MAXIMIZED_BOTH); //Maximiza a tela
        carregaEspecialidade();
        carregaSalas();
        //jCBFKEspecialidade.setVisible(false);
        jTxtCodigo.setVisible(false);

    }

    public CadMedico(Medico med) {
        //this.tp = tp;
        initComponents();
        this.setExtendedState(javax.swing.JFrame.MAXIMIZED_BOTH); //Maximiza a tela
        carregaEspecialidade();
        carregaSalas();
        medico = med;
        mostra();
        jTxtCodigo.setVisible(false);
        //jCBFKEspecialidade.setVisible(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jTxtNome = new javax.swing.JTextField();
        jTxtCRM = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jBtSalvar = new javax.swing.JButton();
        jBtCancelar = new javax.swing.JButton();
        jLbErro = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jTxtEndereco = new javax.swing.JTextField();
        jCKNoite = new javax.swing.JCheckBox();
        jCbEspecialidade = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jCKTarde = new javax.swing.JCheckBox();
        jTxtTelefone = new javax.swing.JFormattedTextField();
        jLabel7 = new javax.swing.JLabel();
        jCKManha = new javax.swing.JCheckBox();
        jBtCadEspecialidade = new javax.swing.JButton();
        jTxtCodigoMed = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jCbSala = new javax.swing.JComboBox();
        jBtCadSala = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        jTxtUsuario = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jTxtSenha = new javax.swing.JTextField();
        jTxtCodigo = new javax.swing.JTextField();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMnAcoes = new javax.swing.JMenu();
        jMiCadastrar = new javax.swing.JMenuItem();
        jMiSair = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastro de Médico");

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/1681153_4222.jpg"))); // NOI18N

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Médico"));

        jLabel2.setText("Nome:");

        jLabel4.setText("CRM:");

        jBtSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/V.png"))); // NOI18N
        jBtSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtSalvarActionPerformed(evt);
            }
        });

        jBtCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/X.png"))); // NOI18N
        jBtCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtCancelarActionPerformed(evt);
            }
        });

        jLbErro.setText("Hostipal Reat In Peace");

        jLabel3.setText("Endereço:");

        jLabel6.setText("Especialidade:");

        jCKNoite.setBackground(new java.awt.Color(255, 255, 255));
        jCKNoite.setText("Noite");

        jCbEspecialidade.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione ...", "Clínico geral", "Cardiologista", "Pediatra", "Nutricionista", "Endocrinologista" }));
        jCbEspecialidade.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jCbEspecialidadeItemStateChanged(evt);
            }
        });

        jLabel5.setText("Telefone:");

        jCKTarde.setBackground(new java.awt.Color(255, 255, 255));
        jCKTarde.setText("Tarde");

        try {
            jTxtTelefone.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##) ####.####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        jLabel7.setText("Turno:");

        jCKManha.setBackground(new java.awt.Color(255, 255, 255));
        jCKManha.setText("Manhã");

        jBtCadEspecialidade.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/cadIco.gif"))); // NOI18N
        jBtCadEspecialidade.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtCadEspecialidadeActionPerformed(evt);
            }
        });

        jTxtCodigoMed.setBackground(new java.awt.Color(204, 204, 204));
        jTxtCodigoMed.setEditable(false);
        jTxtCodigoMed.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jLabel8.setText("Código:");

        jLabel9.setText("Sala:");

        jCbSala.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione ...", "Clínico geral", "Cardiologista", "Pediatra", "Nutricionista", "Endocrinologista" }));
        jCbSala.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jCbSalaItemStateChanged(evt);
            }
        });

        jBtCadSala.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/cadIco.gif"))); // NOI18N
        jBtCadSala.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtCadSalaActionPerformed(evt);
            }
        });

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));
        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Login"));

        jLabel10.setText("Usuário:");

        jLabel11.setText("Senha:");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel10)
                    .addComponent(jLabel11))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTxtUsuario, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
                    .addComponent(jTxtSenha))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTxtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(57, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTxtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10)
                    .addComponent(jTxtCodigo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTxtSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11))
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jLbErro, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(263, 263, 263))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel5))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addComponent(jCKManha)
                                        .addGap(18, 18, 18)
                                        .addComponent(jCKTarde)
                                        .addGap(18, 18, 18)
                                        .addComponent(jCKNoite))
                                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(jTxtEndereco, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jCbEspecialidade, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jTxtTelefone, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel8))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTxtCodigoMed, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(jTxtNome)
                                        .addComponent(jTxtCRM, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jBtCadEspecialidade, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addComponent(jBtCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(31, 31, 31)
                                        .addComponent(jBtSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addComponent(jLabel9)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jCbSala, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jBtCadSala, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(203, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTxtCodigoMed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTxtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTxtCRM, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTxtEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTxtTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCbEspecialidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(jBtCadEspecialidade, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jCKManha)
                    .addComponent(jCKTarde)
                    .addComponent(jCKNoite))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCbSala, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(jBtCadSala, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 47, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jBtCancelar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(jBtSalvar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLbErro)
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 276, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 54, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(62, 62, 62))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(109, 109, 109)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 445, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        jMnAcoes.setText("Ações");

        jMiCadastrar.setText("Cadastrar");
        jMiCadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMiCadastrarActionPerformed(evt);
            }
        });
        jMnAcoes.add(jMiCadastrar);

        jMiSair.setText("Sair");
        jMiSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMiSairActionPerformed(evt);
            }
        });
        jMnAcoes.add(jMiSair);

        jMenuBar1.add(jMnAcoes);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBtSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtSalvarActionPerformed
        if (!verificaBranco()) {
            return;
        }

        Especialidade e = new Especialidade();

        medico.setNome(jTxtNome.getText());
        medico.setCrm(Integer.parseInt(jTxtCRM.getText()));
        medico.setEndereco(jTxtEndereco.getText());
        medico.setTelefone(jTxtTelefone.getText());

        //medico.setEspecialidadeIdespecialidade(Integer.parseInt(jCBFKEspecialidade.getSelectedItem().toString()));
        e = (Especialidade) jCbEspecialidade.getSelectedItem();

        medico.setEspecialidadeIdespecialidade(e.getIdespecialidade());

        medico.setManha(papUtil.salvaBoolean(jCKManha.isSelected()));
        medico.setTarde(papUtil.salvaBoolean(jCKTarde.isSelected()));
        medico.setNoite(papUtil.salvaBoolean(jCKNoite.isSelected()));
        
        if (!jTxtCodigoMed.getText().trim().equals(""))
            medico.setIdmedico(Integer.parseInt(jTxtCodigoMed.getText()));

        sisDao.addUpObjeto(medico); 
        
        int ultCod = (int) sisDao.ultimoCodigo("Modelo.Medico", "idmedico");
        
        l = new Login();
        
        l.setIdRelacao(ultCod);
        l.setSenha(jTxtSenha.getText());
        l.setUsuario(jTxtUsuario.getText());
        l.setCategoria(1);
        
        if (!jTxtCodigo.getText().trim().equals(""))
            l.setIdlogin(Integer.parseInt(jTxtCodigo.getText()));
        
        sisDao.addUpObjeto(l);
        
        this.dispose();

    }//GEN-LAST:event_jBtSalvarActionPerformed

    private void jBtCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtCancelarActionPerformed
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_jBtCancelarActionPerformed

    private void jMiCadastrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMiCadastrarActionPerformed
        if (!verificaBranco()) {
            return;
        }
        jLbErro.setText("Médico cadastrado com sucesso!");
    }//GEN-LAST:event_jMiCadastrarActionPerformed

    private void jMiSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMiSairActionPerformed
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_jMiSairActionPerformed

    private void jCbEspecialidadeItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCbEspecialidadeItemStateChanged
        //jCBFKEspecialidade.setSelectedIndex(jCbEspecialidade.getSelectedIndex());
    }//GEN-LAST:event_jCbEspecialidadeItemStateChanged

    private void jBtCadEspecialidadeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtCadEspecialidadeActionPerformed
        CadEspecialidade cadEsp = new CadEspecialidade(1, this);
        cadEsp.setVisible(true);
    }//GEN-LAST:event_jBtCadEspecialidadeActionPerformed

    private void jCbSalaItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCbSalaItemStateChanged
        // TODO add your handling code here:
    }//GEN-LAST:event_jCbSalaItemStateChanged

    private void jBtCadSalaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtCadSalaActionPerformed
        CadSala cadSal = new CadSala(1, this);
        cadSal.setVisible(true);
    }//GEN-LAST:event_jBtCadSalaActionPerformed

    private boolean verificaBranco() {
        if (jTxtNome.getText().trim().equals("")) {
            jLbErro.setText("Campo nome do médico é obrigatorio.");
            return false;
        }
        if (jTxtCRM.getText().equals("")) {
            jLbErro.setText("Campo CRM é obrigatorio.");
            return false;
        }
        if (jTxtEndereco.getText().trim().equals("")) {
            jLbErro.setText("Campo endereço é obrigatorio.");
            return false;
        }
        if (jTxtTelefone.getText().equals("(  )     .    ")) {
            jLbErro.setText("Campo Telefone é obrigatorio.");
            return false;
        }
        if (jCbEspecialidade.getSelectedIndex() <= 0) {
            jLbErro.setText("Campo Especialidade é obrigatorio.");
            return false;
        }

        return true;
    }

    private void mostra() {
        jTxtCodigoMed.setText(String.valueOf(medico.getIdmedico()));
        jTxtNome.setText(medico.getNome());
        jTxtCRM.setText(medico.getCrm().toString());
        jTxtEndereco.setText(medico.getEndereco());
        jTxtTelefone.setText(medico.getTelefone());

        jCKManha.setSelected(papUtil.mostraBoolean(medico.getManha()));
        jCKNoite.setSelected(papUtil.mostraBoolean(medico.getNoite()));
        jCKTarde.setSelected(papUtil.mostraBoolean(medico.getTarde()));
        //jCBFKEspecialidade.setSelectedItem(medico.getEspecialidadeIdespecialidade());
        
        ArrayList<Login> arrayLogin = new ArrayList<>();

        arrayLogin = (ArrayList<Login>) sisDao.listaCondicao("Modelo.Login",
                String.valueOf(medico.getIdmedico()), null, null, "idRelacao", 3);

        if (!(arrayLogin == null)) {

            for (Login lo : arrayLogin) {
                if (lo.getCategoria() == 1) {
                    jTxtUsuario.setText(lo.getUsuario());
                    jTxtSenha.setText(lo.getSenha());
                    jTxtCodigo.setText(String.valueOf(lo.getIdlogin()));
                }
            }
        }
        
        for (Especialidade esp : arrayEspecialidade) {
            int id = medico.getEspecialidadeIdespecialidade();
            if (id == esp.getIdespecialidade()) {
                jCbEspecialidade.setSelectedItem(esp);
            }
        }

        //jCbEspecialidade.setSelectedIndex();

    }

    public void carregaEspecialidade() {
        arrayEspecialidade = sisDao.listar("Modelo.Especialidade");
        jCbEspecialidade.removeAllItems();
        jCbEspecialidade.addItem("Selecione...");

        for (Especialidade esp : arrayEspecialidade) {
            jCbEspecialidade.addItem(esp);
        }

        jCbEspecialidade.setSelectedIndex(0);
    }
    
    public void carregaSalas(){
        ArrayList<Sala> arraySala = new ArrayList<Sala>();
        
        arraySala = sisDao.listar("Modelo.Sala");
        jCbSala.removeAllItems();
        jCbSala.addItem("Selecione...");

        for (Sala esp : arraySala) {
            jCbSala.addItem(esp);
        }

        jCbSala.setSelectedIndex(0);
    }
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtCadEspecialidade;
    private javax.swing.JButton jBtCadSala;
    private javax.swing.JButton jBtCancelar;
    private javax.swing.JButton jBtSalvar;
    private javax.swing.JCheckBox jCKManha;
    private javax.swing.JCheckBox jCKNoite;
    private javax.swing.JCheckBox jCKTarde;
    private javax.swing.JComboBox jCbEspecialidade;
    private javax.swing.JComboBox jCbSala;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel jLbErro;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMiCadastrar;
    private javax.swing.JMenuItem jMiSair;
    private javax.swing.JMenu jMnAcoes;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField jTxtCRM;
    private javax.swing.JTextField jTxtCodigo;
    private javax.swing.JTextField jTxtCodigoMed;
    private javax.swing.JTextField jTxtEndereco;
    private javax.swing.JTextField jTxtNome;
    private javax.swing.JTextField jTxtSenha;
    private javax.swing.JFormattedTextField jTxtTelefone;
    private javax.swing.JTextField jTxtUsuario;
    // End of variables declaration//GEN-END:variables
}
